package com.chezhujie.www.controller;

import com.chezhujie.www.constant.PageInfo;
import com.chezhujie.www.core.page.Pagination;
import com.chezhujie.www.domain.Czjuserinfo;
import com.chezhujie.www.domain.Czjusermess;
import com.chezhujie.www.domain.vo.userinfoMessVO;
import com.chezhujie.www.service.CzjuserinfoService;
import com.chezhujie.www.service.CzjusermessService;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/userinfo")
public class CzjuserinfoAndMesController extends BaseController {

	private static final Logger log = LoggerFactory
			.getLogger(CzjuserinfoAndMesController.class);

		//显示所有用户
		@RequestMapping("list")
		public String login(HttpServletRequest request,Czjuserinfo czjuserinfo, String vip,HttpServletResponse response,
				Integer pageNo, Integer pageSize,
				ModelMap model) throws Exception {
	
		      if (pageNo == null) {
		            pageNo = PageInfo.DEFAULT_PAGENO;
		        }
		        if (pageSize == null) {
		            pageSize = PageInfo.DEFAULT_PAGESIZE;
		        }
		        Map<String,Object> map=new HashMap<String, Object>();
		        if(null != czjuserinfo.getTelephone()){
		        	map.put("telephone", czjuserinfo.getTelephone());
		        	model.addAttribute("telephone", czjuserinfo.getTelephone());
		        }
		        if(null != czjuserinfo.getCarplate()){
		        	map.put("carplate", czjuserinfo.getCarplate());
		        	model.addAttribute("carplate", czjuserinfo.getCarplate());
		        }
		        if(StringUtils.isNotBlank(vip)){
		        	map.put("vip", vip);
		        	model.addAttribute("vip", vip);
		        }
			try {
				// 查询所有的会员和非会员
//				List<Integer> vips=czjuserinfoService.selectVip();
				int vip1=czjuserinfoService.selectVip1();
				int vip2=czjuserinfoService.selectVip2();
				model.addAttribute("vip1", vip1);
				model.addAttribute("vip2", vip2);
				
				int sunUser=czjuserinfoService.getSumCount();
				model.addAttribute("sunUser", sunUser);
				//
				Pagination pagination=czjuserinfoService.getCzjuserAndMes(map,pageNo,pageSize);
				model.addAttribute("pagination", pagination);
				
			} catch (Exception e) {
				e.printStackTrace();
				log.error("error", e);
	
			}
	
			return "userinfo/list";
		}
	
		//禁止用户登陆
		@RequestMapping("prohibit")
		public String prohibit(@RequestParam(required=true) Integer userinfoid, 
				@RequestParam(required=true) String enable,
				HttpServletRequest request, HttpServletResponse response,
				ModelMap model) throws Exception {

			try {
				Czjuserinfo czjuserinfo=new Czjuserinfo();
				czjuserinfo.setId(userinfoid);
				czjuserinfo.setEnable(enable);
				czjuserinfoService.updateCzjuserinfo(czjuserinfo);
				
			} catch (Exception e) {
				e.printStackTrace();
				log.error("error", e);

			}

			return "redirect:/userinfo/list";
		}
		
		//禁止用户评论
				@RequestMapping("noComment")
				public String noComment(@RequestParam(required=true) String usermessId, @RequestParam(required=true) Integer userinfoid, 
						@RequestParam(required=true) String noComment,
						HttpServletRequest request, HttpServletResponse response,
						ModelMap model) throws Exception {

					try {
						if(StringUtils.isBlank(usermessId)){
							
							Czjusermess czjusermess=new Czjusermess();
							czjusermess.setNoComment(noComment);
							czjusermess.setUserinfoId(userinfoid);
							czjusermessService.insertUsermess(czjusermess);
							
							
						}else{
							
						
						Czjusermess czjusermess=new Czjusermess();
						czjusermess.setUsermessId(usermessId);
						czjusermess.setNoComment(noComment);
						czjusermessService.updateUsermess(czjusermess);
						}
						
					} catch (Exception e) {
						e.printStackTrace();
						log.error("error", e);

					}

					return "redirect:/userinfo/list";
				}
		
		//查看详情
				@RequestMapping("detail")
				public String detail(@RequestParam(required=true) Integer userinfoid, HttpServletRequest request, HttpServletResponse response,
						ModelMap model) throws Exception {

					try {

						Map<String, Object> map=new HashMap<String, Object>();
						
							map.put("id", userinfoid);
						userinfoMessVO messVO=czjuserinfoService.getCzjuserAndMesById(map);
						model.addAttribute("messVO", messVO);
					} catch (Exception e) {
						e.printStackTrace();
						log.error("error", e);

					}
						return "userinfo/detail";
				}


	@Resource
	private CzjuserinfoService czjuserinfoService;
	@Resource
	private CzjusermessService czjusermessService;

}